﻿using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;
using HRMS_Model;
using HRMS.Models;
using System.Web.UI;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Lib.Web.Mvc.JQuery.JqGrid;

namespace HRMS.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Employees(JqGridRequest request)
        {
            //get the count of employees
            var totalRecords = EmployeeModels.GetEmployeesCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get employees
            var employees = EmployeeModels.GetEmployees(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var employee in employees)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(employee.EmpId),
                    new List<object> { 
                                    employee.EmpId,
                employee.FirstName,
                employee.MiddleName,
                employee.LastName,
                employee.Address,
                employee.City,
                employee.State,
                employee.Zip,
                employee.MobileNo,
                employee.Extension,
                employee.HomePhone,
                employee.Email,
                employee.Gender,
                employee.DateOfBirth,
                employee.MartialStatus,
                employee.DegreeAttained,
                employee.Skills,
                employee.Experience,
                employee.ResumeSize,
                employee.Resume,
                employee.Designation.DesignationName,
                employee.Department.DepartmentName
                    }
                ));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertEmployee([ModelBinder(typeof(CustomModelBinder))]Employee empModel)
        {
            return Json(EmployeeModels.AddEmployee(empModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateEmployee([ModelBinder(typeof(CustomModelBinder))]Employee empModel)
        {
            return Json(EmployeeModels.UpdateEmployee(empModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteEmployee(int id)
        {
            return Json(EmployeeModels.DeleteEmployee(id));
        }

        public ActionResult GetDesignations()
        {
            return Json(EmployeeModels.DesignationsList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetDepartments()
        {
            return Json(DepartmentsModel.DepartmentsList(), JsonRequestBehavior.AllowGet);
        }
    }
}
